﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Blog.Common.Helper;
using Blog.Common.Redis;
using Blog.Core.Base;
using Blog.Core.IServices;
using Blog.Core.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;


namespace Blog.Core.Controllers
{
    [Produces("application/json")]
    [Route("api/[controller]")]
    [ApiController]
    public class BlogController : ControllerBase
    {
        // GET: api/Blog

        private IAdvertisementServices _advertisementServices;
        // controller 接口调用
        private IBlogArticleServices _blogArticleServices;

        // 调用HttpClientFactory 请求类
        private readonly IHttpClientFactory _httpClient;
        // 自定义HttpClientFactory 请求类
        private readonly TestSampleClient _testsampleClient;
        // 完全自定义HttpClientFactory 请求类
        private readonly ISampleClient _sampleClient;

        private RedisCacheManager redisCacheManager;

        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="advertisementServices"></param>
        public BlogController(IAdvertisementServices advertisementServices, IBlogArticleServices blogArticleServices, IHttpClientFactory httpClient,TestSampleClient testsampleClient,ISampleClient sampleClient)
        {
            _advertisementServices = advertisementServices;
            _blogArticleServices = blogArticleServices;
            _httpClient = httpClient;
            _testsampleClient = testsampleClient;
            _sampleClient = sampleClient;
        }



        // GET: api/Blog/5
        [HttpGet, Route("GetTest")]

        public List<Advertisement> GetTest(int id)
        {
            return _advertisementServices.Query(s => s.Id == id);
        }

        [HttpGet("{id}", Name = "Get")]
        public async Task<object> Get(int id)
        {
            var model = await _blogArticleServices.getBlogDetails(id);//调用该方法，这里 _blogArticleServices 是依赖注入的实例，不是类
            var data = new { success = true, data = model };
            return data;
        }


        /// <summary>
        /// 获取博客列表
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("GetBlogs")]
        public async Task<List<BlogArticle>> GetBlogs()
        {
            var connect = Appsettings.app(new string[] { "AppSettings", "RedisCaching", "ConnectionString" });//按照层级的顺序，依次写出来
            return await _blogArticleServices.GetBlogs();
        }

        /// <summary>
        /// 获取博客信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}", Name = "GetArticle")]
        public async Task<object> GetArticle(int id)
        {
            var model = await _blogArticleServices.getBlogDetails(id);//调用该方法
            var data = new { success = true, data = model };
            return data;
        }

        /// <summary>
        /// 获取博客列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetBlogList")]
        public async Task<List<BlogArticle>> GetBlogList()
        {
            var connect = Appsettings.app(new string[] { "AppSettings", "RedisCaching", "ConnectionString" });//按照层级的顺序，依次写出来

            List<BlogArticle> blogArticleList = new List<BlogArticle>();

            if (redisCacheManager.Get<object>("Redis.Blog") != null)
            {
                blogArticleList = redisCacheManager.Get<List<BlogArticle>>("Redis.Blog");
            }
            else
            {
                blogArticleList = await _blogArticleServices.Query(d => d.bID > 5);
                redisCacheManager.Set("Redis.Blog", blogArticleList, TimeSpan.FromHours(2));//缓存2小时
            }

            return blogArticleList;
        }

        /// <summary>
        /// HttpClient 工厂类的使用
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("TestHttp")]
        public async Task<ActionResult> TestHttp()
        {
            var client = _httpClient.CreateClient("client_1"); //复用在Startup中定义的client_1的httpclient
            var result = await client.GetStringAsync("/page1.html");

            var client2 = _httpClient.CreateClient(); //新建一个HttpClient
            var result2 = await client.GetStringAsync("http://www.site.com/XXX.html");

            return null;
        }

        /// <summary>
        /// 测试自定义HttpClientFactory请求类
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("GetTestsmp")]
        public async Task<ActionResult> GetTestsmp()
        {
            string result = await _testsampleClient.Client.GetStringAsync("/");
            return Ok(result);
        }


        /// <summary>
        /// 测试完全自定义HttpClientFactory请求类
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("GetSample")]
        public async Task<ActionResult> GetSample()
        {
            string result = await _sampleClient.GetData();
            return Ok(result);
        }
    }
}
